package cz.moglum.bloodb.client.ui;

import com.google.gwt.user.client.ui.VerticalPanel;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.TextArea;
import com.gwtext.client.widgets.form.TextField;
import com.gwtext.client.widgets.layout.VerticalLayout;

import cz.moglum.bloodb.client.i.IObjectFactoryListener;
import cz.moglum.bloodb.client.i.IObjectListener;
import cz.moglum.bloodb.client.objects.BaseObject;
import cz.moglum.bloodb.client.objects.RPCObjectFactory;
import cz.moglum.bloodb.client.objects.TeamObject;

/**
 * The main view for BlooDB
 * 
 * @author Vojtech.Polak
 */
public class BlooDBView extends Panel implements IObjectFactoryListener {

	// object factory
	private RPCObjectFactory objectFactory;

	// intialize the view
	public BlooDBView() {
		super();
		this.setBorder(false);
		this.setPaddings(15);
		
		VerticalPanel verticalPanel = new VerticalPanel();
		verticalPanel.setSpacing(5);		

		final TextArea txtResult = new TextArea("Result");
		final TextField txtTeamId = new TextField("Team id");
		final Button clickMe = new Button("Click Me");
		final Button clickClear = new Button("Clear");
		
		txtResult.setDisabled(true);
		txtResult.setWidth(200);
		txtResult.setEmptyText("no results, yet");
		txtResult.setGrow(true);

		verticalPanel.add(txtResult);
		verticalPanel.add(txtTeamId);
		verticalPanel.add(clickMe);
		verticalPanel.add(clickClear);
		
				this.add(verticalPanel);

		/**
		 * TextArea listener, listens for TeamObjects and displays their coach name
		 * 
		 */
		class DisplayResultTeamCoach implements IObjectListener {

			private TextArea field = null;

			public DisplayResultTeamCoach(TextArea field) {
				this.field = field;
			}

			public void onObject(BaseObject result) {
				if (result.getClass() == TeamObject.class) {
					TeamObject teamResult = (TeamObject) result;
					String resultText = "Team: " + teamResult.getTeamName() + "\nCoach: "
							+ teamResult.getTeamCoach();
					field.setValue(resultText);
				}
			}
		}

		// click the button to request a team via the TeamDAO
		clickMe.addListener(new ButtonListenerAdapter() {
			@Override
			public void onClick(Button button, EventObject e) {
				// creates a listener
				DisplayResultTeamCoach listener = new DisplayResultTeamCoach(txtResult);
				// and request a team
				objectFactory.getTeamDAO().getById(txtTeamId.getValueAsString(), listener);
			}
		});
		
		// click the Clear button to clear the result area
		clickClear.addListener(new ButtonListenerAdapter(){
			@Override
			public void onClick(Button button, EventObject e) {
				txtResult.setValue("");
			}
		});
	}

	public void setObjectFactory(RPCObjectFactory objectFactory) {
		this.objectFactory = objectFactory;
	}

	public void onError(String error) {
		// TODO Auto-generated method stub

	}

	public void onLoadingFinish() {
		// TODO Auto-generated method stub

	}

	public void onLoadingStart() {
		// TODO Auto-generated method stub

	}

	public void onRefresh() {
		// TODO Auto-generated method stub

	}

}
